﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using lucienneCRMApp.Models;
using Devtalk.EF.CodeFirst;
using Graphite.StatsD;
using Graphite;
using System.Configuration;
using System.Net;
using System.Text;
using System.Net.Sockets;
using StatsdClient;
using metrics;
using StatsMix;


namespace lucienneCRMApp
{
 

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            //Database.SetInitializer<lucienneCRMAppContext>(new Devtalk.EF.CodeFirst.DontDropDbJustCreateTablesIfModelChanged<lucienneCRMAppContext>());
            Database.SetInitializer(new DontDropDbJustCreateTablesIfModelChanged<lucienneCRMAppContext>());
            
            //aspnet Statistics
            metrics.AspNetMvc.Metrics.HealthCheckPath = "my-healthcheck-uri";
            metrics.AspNetMvc.Metrics.PingPath = "my-ping-uri";
            metrics.AspNetMvc.Metrics.MetricsPath = "my-metrics-uri";
            metrics.AspNetMvc.Metrics.ThreadsPath = "my-threads-uri";
            metrics.AspNetMvc.Metrics.RegisterRoutes();

            //hosted graphite statistics
            var endPoint = new IPEndPoint(Dns.GetHostAddresses("carbon.hostedgraphite.com")[0], 2003);
            var bytes = Encoding.ASCII.GetBytes("ed20701d-66f9-4cc6-ba96-1f8a1f7069c0.conc_users 1215\n");
            var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) { Blocking = false };
            sock.SendTo(bytes, endPoint);
            
            /*
            var apiKey = ConfigurationManager.AppSettings.Get("ed20701d-66f9-4cc6-ba96-1f8a1f7069c0");
            using (var client = new GraphiteTcpClient("carbon.hostedgraphite.com",
                port: 2003, keyPrefix: string.Format("{0}", apiKey)))
            {
                client.Send("users", users.Count()); //counter had one hit
            }
            */
            //using StatsMix
            Client smClient = new Client("09351f00416b3278d10a");
            int count = 0;
            smClient.track("Performance", 1);
                      
            var properties = new Dictionary<string, string>();
            string gen_at = DateTime.Now.ToString();
            properties.Add("generated_at", gen_at);
            smClient.track("User Logins by Plan Type", properties);
            smClient.track("User Logins by Plan Type", 1);
            smClient.track("Users", count++);

            // to add metadata, create a Dictionary object containing key-value pairs
            //var meta = new Dictionary();
            //meta.Add("food", "ice cream");
            //meta.Add("calories", 500);
            //smClient.track("Metric Name", 1, meta);	

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //to send metrics via UDP
            StatsdUDP udp = new StatsdUDP("localhost", 8125);
            using (udp)
            {
                Statsd s = new Statsd(udp);

                //All the standard Statsd message types:
                s.Send<Statsd.Counting>("stat-count", 1); //counter had one hit
                s.Send<Statsd.Timing>("stat-time", 5); //timer had one hit of 5ms
                s.Send<Statsd.Gauge>("stat-gauge", 5); //gauge had one hit of value 5

                //All types have sample rate, which will be included in the message for Statsd's own stats crunching:
                s.Send<Statsd.Counting>("stat-name", 1, 1 / 10); //counter had one hit, this will be sent 10% of times it is called

                //You can add combinations of messages which will be sent in one go:
                //s.Add<Statsd.Counting>("stat-name", 1);
                //s.Add<Statsd.Timer>("stat-name", 5, 1 / 10);
                s.Send(); //message will contain counter and will contain timer 10% of the time

             


            }
         
           
        }
    }
}